<?php defined('SYSPATH') OR die('No direct access allowed.');

class Content_Model extends ORM_Tree{
	
	protected $has_many=array('medias'=>'contmedlinks', 'comment' );
	protected $has_one = array('contentmodule', 'collection');
	protected $belongs_to = array('page', 'collection');
	protected $has_and_belongs_to_many=array('contcategories');
	protected $children = 'contents'; 
	
	public function unique_key($id = NULL){
		if ( ! empty($id) AND is_string($id) AND ! ctype_digit($id) ){
			return 'shortname';
		}
		return parent::unique_key($id);
	}

	public function shortname_check($shortname){
		if($shortname==''){
			return false;		
		}
		$parray=ORM::factory('content')->where(array('shortname'=>$shortname, 'id!='=>$this->id))->find_all()->as_array();
		if(count($parray)>0){
			return false;
		}
		return true;
	}
	
	public function get_nice_date(){
		if($this->date==''){
			return;
		}else{
			$datearray=explode('/',$this->date);
			$y=(array_key_exists(0, $datearray)?$datearray[0]:'');
			$m=(array_key_exists(1, $datearray)?$datearray[1]:'');
			$d=(array_key_exists(2, $datearray)?$datearray[2]:'');
			return $d.'.'.$m.'.'.$y;
		}
	}
	
	
	public function get_year(){
		$year=0;
		
		if($this->date!=''){
			$datearray=explode('/',$this->date);
			$year=(array_key_exists(0, $datearray)?$datearray[0]:'');	
		}
		return $year;
	}
	
	public function get_id_image($var, $w, $h){
		$r="";
		if($var){
			$media=ORM::factory('media', $var);
			$url=$media->get_resized_url($w, $h);
			$r=html::image($url);		
		}
		return $r;
	}
	public function get_id_image_url($var, $w, $h){
		$media=ORM::factory('media', $var);
		$url=$media->get_resized_url($w, $h);
		return $url;
	}
	
	public function get_id_media($var){
		$media=ORM::factory('media', $var);
		return $media;
	}
	

}